package Navi_run;

import objects.LocationBean;


import it.unisa.dia.gas.jpbc.Element;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.*;

import static Navi_run.Navigation_verify.*;
import static Navi_run.driver_generate.*;
import static Navi_run.entity_register.TestZPKP;

public class main {

    public static void circle() {
        Calendar calendar = Calendar.getInstance();
        Date firstTime = calendar.getTime();
        // 间隔：2分钟
        long period = 1000 * 2;
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("0000"+firstTime.toString()+"***"+period);
            }
        }, firstTime, period);
    }


    //整体演示
    //各模块演示可见各模块main方法
    public static void main(String[] args) {

        circle();
        System.out.println("89798987987978");

    try{
        SecureRandom secureRandom = new SecureRandom();
        BigInteger e = new BigInteger(256, secureRandom);
        BigInteger s = new BigInteger(256, secureRandom);
        BigInteger v = new BigInteger(256, secureRandom);
        BigInteger F = new BigInteger(256, secureRandom);
        TestZPKP(e, s, v, F);
        try {
            //TA初始化 发布各种参数
            initialize initialize = new initialize();
            //司机发起注册请求
            entity_register.driverSendRegisterRequest();
            //服务器响应司机注册
            entity_register.TaResponseDriverRegist();
            //司机收到服务器响应结果
            entity_register.driver_receive();

            List<LocationBean> locationBeans = new ArrayList<LocationBean>();
            LocationBean locationBean = new LocationBean(35.665553, 48.6666456);
            locationBeans.add(locationBean);
            locationBean.setLat(35.665543);
            locationBean.setLng(35.665543);
            locationBeans.add(locationBean);
            //司机发起导航查询
            Map<String, Object> NQ;
            NQ = generate_Query(locationBeans, TA_driver.SK1, TA_driver.SK2, TA_driver.V, TA_driver.si, 155555, TA_driver.rci, params.ZKPK_rou, params.ZKPK_F, TA_driver.driver_Ei1, TA_driver.driver_Ei2);
            //RSU验证司机的导航查询有效性
            verifyZKPK((Map<String, BigInteger>) NQ.get("ZKPK"), params.ZKPK_F);
            verifyRequest((Element) NQ.get("REi1"), (Element) NQ.get("REi2"), params.gpk_A, params.gpk_B, params.gpk_g, (Map<String, Object>) NQ.get("Mi"), (BigInteger) NQ.get("ci"), (BigInteger) NQ.get("ssi"), TA_driver.rci - 1);

            //生成发送报告要用的hsvjs和pidjs集合
            String daiertaru = "123456";
            for (LocationBean location : locationBeans) {
                generate_pidj(location, 155555, secureRandom.nextInt(), daiertaru);
                generate_hsvj(location, secureRandom.nextInt(), 155555, daiertaru);
            }
//            System.out.println(Arrays.toString(new List[]{params.hsvjs}));
//            System.out.println(Arrays.toString(new List[]{params.pidjs}));
            //生成司机报告Mj
            generate_Mj(locationBean, params.hsvj, params.pidjs, params.hsvjs);
            Map<String, Object> Mj;
            Mj = generate_Mj(params.indj, params.hsvj, params.pidjs, params.hsvjs);
            //生成要发给DR的数据
            Map<String, Object> DR;
            DR = generate_report(locationBean, Mj, TA_driver.SK1, TA_driver.SK2, TA_driver.V, TA_driver.si, 155555, TA_driver.rci, params.ZKPK_rou, params.ZKPK_F, TA_driver.Ei1, TA_driver.Ei2);
            //RSU验证
            verifyZKPK((Map<String, BigInteger>) DR.get("ZKPK"), params.ZKPK_F);
            verifyReport((Element) DR.get("REi1"), (Element) DR.get("REi2"), params.gpk_A, params.gpk_B, params.gpk_g, (Map<String, Object>) DR.get("Mj"), (BigInteger) DR.get("ci"), (BigInteger) DR.get("ssi"), TA_driver.rci - 1);

            Navigation_query.driver_generate_NQ("12345678", TA_driver.rci, TA_driver.si, Navigation_query.driver_generate_query(locationBeans, true), true);
//            //RSU/NSP验证导航查询
            Navigation_respond.verifyRequest();






            //RSU/NSP广播交通拥堵任务
            Navi_run.Navigation_respond.broadcastTask();
//            //cdi发起邻近挑战
//            Navi_run.Navigation_respond.broadchallenge();
//            //cdi发送报告给RSU/NSP
//            Navi_run.Navigation_respond.sendReported(query);
//            //验证cdi报告时用验证rdi请求的方法即可
//            //RSU/NSP 生成导航数据库
//            Navi_run.Navigation_respond.gnerateDatabases();
            //RSU/NSP将恶意司机签名发给TA
            DriverTrack.sendSignToTA();
            //TA查找恶意用户
            DriverTrack.TAresponse();
        } catch (Exception exception) {
            System.out.println("出现异常" + exception);
        }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
